package com.coocaa.x.app.hall.view.background;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;

import com.coocaa.x.app.hall.R;
import com.coocaa.x.app.hall.view.IViewComponent;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.utils.CoocaaFresco;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.BasePostprocessor;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.facebook.imagepipeline.request.Postprocessor;

/**
 * Created by sunny on 15-6-17.
 */
public class BackgroundComponent implements IViewComponent {
    protected BackgroundView backgroundView;
    protected Context mContext = null;
    protected boolean isUpload = false;
    protected ImageSetOverListener imageSetOverLisenter;
    CoocaaDraweeView mSimpleDraweeView;
    Uri uri;
    Object data;
    AnimationDrawable frameAnimation;
    Drawable bgDrawable;

    public interface ImageSetOverListener {
        public void imageSetOver(Bitmap bitmap);
    }

    public BackgroundComponent(Context context) {
        this.mContext = context;
        backgroundView = new BackgroundView(mContext);
        addBgFrame();
    }

    public void onResume() {
        if (!isUpload && mSimpleDraweeView != null) {
            mSimpleDraweeView
                    .getHierarchy().setPlaceholderImage(frameAnimation);
            mSimpleDraweeView.setImageURI(uri);
            if (!frameAnimation.isRunning()) {
                frameAnimation.start();
            }
        } else if (mSimpleDraweeView == null && data != null) {
            setData(data);
        }
    }

    @Override
    public void destroy() {

        imageSetOverLisenter = null;

        backgroundView.destroy();
        if (mSimpleDraweeView != null) {
            mSimpleDraweeView.destroyDrawingCache();
            mSimpleDraweeView = null;
        }
    }

    @Override
    public View getView() {
        return backgroundView;
    }

    @Override
    public void setData(Object data) {

        if (data == null)
            return;

        this.data = data;
        isUpload = false;
        String path = (String) data;
        LogUtils.i("sunny BackgroundComponent setData:" + path);

        mSimpleDraweeView = new CoocaaDraweeView(mContext);
        RoundingParams roundingParams = RoundingParams.fromCornersRadius(12f);
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(mContext.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
                .setFadeDuration(300)
                .setPlaceholderImage(frameAnimation)
                .setBackground(bgDrawable)
                .setRoundingParams(roundingParams)
                .build();
        mSimpleDraweeView.setHierarchy(hierarchy);

        uri = Uri.parse(path);
        int width = CoocaaApplication.Div(635), height = CoocaaApplication.Div(635);
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setProgressiveRenderingEnabled(true)
                .setPostprocessor(redMeshPostprocessor)
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        DraweeController controller = CoocaaFresco.newDraweeControllerBuilder(mContext)
                .setControllerListener(controllerListener)
                .setOldController(mSimpleDraweeView.getController())
                .setImageRequest(request)
                .build();
        mSimpleDraweeView.setController(controller);

        backgroundView.addView(mSimpleDraweeView);
        if (!frameAnimation.isRunning()) {
            frameAnimation.start();
        }
    }

    public void setImageSetOverLisenter(ImageSetOverListener lisenter) {
        this.imageSetOverLisenter = lisenter;
    }

    Postprocessor redMeshPostprocessor = new BasePostprocessor() {
        @Override
        public String getName() {
            return "redMeshPostprocessor";
        }

        @Override
        public void process(Bitmap bitmap) {
            if (imageSetOverLisenter != null)
                imageSetOverLisenter.imageSetOver(bitmap);
        }
    };

    ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
        @Override
        public void onSubmit(String s, Object o) {
            isUpload = true;
            LogUtils.i("facebook", "sunny facebook onSubmit s:" + s);
        }

        @Override
        public void onFinalImageSet(String s, ImageInfo o, Animatable animatable) {
            LogUtils.i("facebook", "sunny facebook onFinalImageSet s:" + s + ";   o:" + o.toString());
        }

        @Override
        public void onIntermediateImageFailed(String s, Throwable throwable) {
            LogUtils.i("facebook", "sunny facebook onIntermediateImageFailed s:" + s + ";   throwable:" + throwable.toString());
        }

        @Override
        public void onFailure(String s, Throwable throwable) {
            LogUtils.i("facebook", "sunny facebook onFailure s:" + s + ";   throwable:" + throwable.toString());
            mSimpleDraweeView.setImageURI(uri);
        }

        @Override
        public void onRelease(String s) {
            LogUtils.i("facebook", "sunny facebook onRelease s:" + s);
        }
    };

    private void addBgFrame() {
        frameAnimation = (AnimationDrawable) mContext.getResources().getDrawable(
                R.drawable.drawable_anim);
        bgDrawable = mContext.getResources().getDrawable(R.drawable.place_bg);
    }
}
